package demo;

public class MyQueue {
    public static class ListNode {
        ListNode next;
        ListNode prev;
        int value;

        ListNode(int value) {
            this.value = value;
        }
    }

    ListNode first; // 队头
    ListNode last; // 队尾
    int size = 0;

    public void offer(int val) {
        ListNode cur=new ListNode(val);
        if(first==null){
            first=last=cur;
        }
        last.next=cur;
        cur.prev=last;
        cur.next=null;
        last=cur;
        size++;
    }

    public int poll() {
        ListNode cur=first;
        if(first==null){
            throw new EmptyException();
        }else if(first==last){
            size--;
            return first.value;
        }else {
            first=first.next;
            first.prev=null;
            size--;
            return cur.value;
        }
    }

    public int peek() {
        if(first==null){
            throw new EmptyException();
        }else{
            return first.value;
        }
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return first==null;
    }

}
